/*
 * Copyright (C) 2011-2012 Andrew Wells
 *
 * Licensed under the GNU License, Version 3
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Dungeons & Dragons Copyright (C) Wizards of the Coast. I do not claim
 * the right to any information used from their books or resources.
 */

package com.trace.wells.charactertracker;

import java.util.ArrayList;
import java.util.UUID;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewCustomAdapter extends BaseAdapter {

	private ArrayList<String> characterInfoString;

	public Activity context;
	public LayoutInflater inflater;
	public int uniqueId;

	public ListViewCustomAdapter(Activity context, ArrayList<String> itemList) {
		super();

		this.context = context;
		this.characterInfoString = itemList;

		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * @return the characterList
	 */
	public ArrayList<String> getCharacterList() {
		return characterInfoString;
	}

	/**
	 * @param characterList
	 *            the characterList to set
	 */
	public void setCharacterList(ArrayList<String> characterList) {
		this.characterInfoString = characterList;
	}

	@Override
	public int getCount() {
		return characterInfoString.size();
	}

	@Override
	public String getItem(int position) {
		return characterInfoString.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public String[] getCharacterInfo(int uniqueId) {
		String characterString = "";
		for (String s : characterInfoString) {
			if (s.contains(String.valueOf(uniqueId))) {
				characterString = s;
				break;
			}
		}

		String delimiter = "\\:";

		String[] split = characterString.split(delimiter);

		return split;
	}

	public static class ViewHolder {
		TextView txtCharName;
		TextView txtLevel;
		TextView txtRace;
		TextView txtClass;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.maincharselect, null);

			holder.txtCharName = (TextView) convertView.findViewById(R.id.txtSelectCharName);
			holder.txtLevel = (TextView) convertView.findViewById(R.id.txtSelectCharLevel);
			holder.txtRace = (TextView) convertView.findViewById(R.id.txtSelectRace);
			holder.txtClass = (TextView) convertView.findViewById(R.id.txtSelectClass);

			convertView.setTag(holder);
		} else
			holder = (ViewHolder) convertView.getTag();

		String characterString = (String) characterInfoString.get(position);

		String delimiter = "\\:";

		String[] split = characterString.split(delimiter);

		holder.txtCharName.setText(split[0]);
		holder.txtLevel.setText("Level " + split[1]);
		holder.txtRace.setText(split[2]);
		holder.txtClass.setText(split[3]);

		return convertView;
	}

	public void removeCharacter(int id) {
		uniqueId = id;
		for (String s : characterInfoString) {
			if (s.contains(String.valueOf(id)))
				characterInfoString.remove(s);
			//Log.d("CHAR", ch.info.getName() + " was deleted");
			break;
		}
	}

}